<template>
    <div>
        <!-- 新增/编辑指标项 -->
        <Modal
            :closable="false"
            :mask-closable="false"
            v-model="RiskToolSlideModal.show"
            :title="RiskToolSlideModal.title">
             <Form :model="RiskToolSlideModal.form" label-position="right" :label-width="100">
                <FormItem label="筛选工具类型" class="mb-0">
                    <RadioGroup v-model="RiskToolSlideModal.form.type" @on-change="switchRiskTool">
                        <Radio label="scoreCard">
                            <span>打分卡</span>
                        </Radio>
                        <Radio label="alertRule">
                            <span>预警规则</span>
                        </Radio>
                    </RadioGroup>
                </FormItem>
                 <FormItem label="筛选所属对象" class="mb-0">
                    <Select v-model="RiskToolSlideModal.form.applyObject" disabled >
                        <Option 
                            v-for="item in RiskToolSlideModal.applyObjectList" 
                            :value="item.code" 
                            :disabled="item.name !== 'customer'"
                            :key="item.code">{{ item.name }}</Option>
                    </Select>
                </FormItem>
                <FormItem label="选择风控工具" class="mb-0">
                    <Select v-model="RiskToolSlideModal.form.riskTool" >
                        <Option 
                            v-for="item in RiskToolSlideModal.riskToolList" 
                            :value="item.oid" 
                            :key="item.oid">{{ item.name }}</Option>
                    </Select>
                </FormItem>
                <FormItem label="设置是否启用" class="mb-0">
                     <Switch v-model="RiskToolSlideModal.form.switch"></Switch>
                </FormItem>
                <FormItem label="设置定时模式" class="mb-0">
                    <Select v-model="RiskToolSlideModal.form.jobPattern" >
                        <Option 
                            v-for="item in RiskToolSlideModal.jobPatternList" 
                            :value="item.udcValue" 
                            :key="item.udcValue">{{ item.udcValueDesc }}</Option>
                    </Select>
                </FormItem>
            </Form>

            <div slot="footer">
                <Button 
                    type="primary" class="mr-2" 
                    :loading="RiskToolSlideModal.loading"
                    :disabled="!RiskToolSlideModal.form.riskTool"
                    @click="() => {
                            let self = this;
                            let modal = self.RiskToolSlideModal;
                            
                            modal.loading = true;
                            if(modal.isAdd){
                                self.addRiskTool().then(function(res){
                                    // 成功提示
                                    self.$Message.success('新增成功');
                                    // 刷新数据
                                    self.table.data = [];
                                    modal.loading = false;
                                    self.loadRiskTools().then((res) => {
                                        self.table.loading = false;
                                        self.table.data = res.data.data ? res.data.data : [];
                                    });

                                    // 清空表单
                                    modal.form = {
                                        type: 'scoreCard',
                                        riskTool: null,
                                        switch: true,
                                        applyObject: 'customer',
                                        jobPattern: ''
                                    };
                                    // 隐藏弹窗
                                    modal.show = false;
                                });
                            }else{
                                self.editRiskTool().then(function(){
                                    // 成功提示
                                    self.$Message.success('编辑成功');
                                    // 刷新数据
                                    self.table.data = [];
                                    modal.loading = false;
                                    self.loadRiskTools().then((res) => {
                                        self.table.loading = false;
                                        self.table.data = res.data.data ? res.data.data : [];
                                    });
                                    // 清空表单
                                    modal.form = {
                                        type: 'scoreCard',
                                        riskTool: null,
                                        switch: true,
                                        applyObject: 'customer',
                                        jobPattern: '每日执行'
                                    };
                                    // 隐藏弹窗
                                    modal.show = false;
                                });
                            }
                        }" >确定</Button>
                <Button type="ghost"  
                     @click="() => {
                        let self = this;
                        let modal = self.RiskToolSlideModal;
                        // 清空表单
                        modal.form = {
                            type: 'scoreCard',
                            riskTool: null,
                            switch: true,
                            applyObject: 'customer',
                            jobPattern: '每日执行'
                        };
                        // 隐藏弹窗
                        modal.show = false;
                    }">取消</Button>
            </div>
        </Modal>

        <b-card no-body class="mb-2">
            <b-card-header header-tag="h6" text-variant="white" class="with-elements pr-0 pb-0">
                <div class="card-header-title">风控工具列表</div>
                <div class="card-header-elements ml-auto mr-4">
                    <Button type="ghost" size="small" @click="() => {
                        let modal = this.RiskToolSlideModal;

                        modal.title = '新增风控工具';
                        modal.isAdd = true;
                        modal.show = true;

                    }">添加风控工具</Button>
                </div>
            </b-card-header>
            <b-card-body>
                <Table size="small" stripe border :columns="table.column" :data="table.data" :loading="table.loading"/>
            </b-card-body>
        </b-card>

    </div>
</template>

<style scoped>
.expand-row {
    margin-bottom: 16px;
}

.ivu-radio-group {
    line-height: 24px;
    height: 24px;
}

div.ivu-table-body.ivu-table-overflowX {
    overflow: hidden !important;
}
</style>

<script>
export default {
    props: {
        row: Object
    },
    data() {
        return {
            RiskToolSlideModal:{
                show: false,
                title: '新增风控工具',
                isAdd: false,
                loading: false,
                riskToolList: [],
                applyObjectList: [],
                jobPatternList: [],
                form: {
                    relationOid: null,
                    type: 'scoreCard',
                    riskTool: '',
                    switch: true,
                    applyObject: 'customer',
                    jobPattern: '每日执行'
                },
            },
            table: {
                loading: false,
                data: [],
                column: [
                    {
                        title: "名称",
                        width: 200,
                        render: (h,context) => {
                            return (
                                <span>{context.row.riskTool.name}</span>
                            )
                        }
                    },
                    {
                        title: "风控对象类型",
                        key: "type",
                        width: 160,
                        align: "center",
                        render: (h,context) => {
                            return (
                                <span>企业客户</span>
                            )
                        }
                    },
                    {
                        title: "风控工具类型",
                        key: "type",
                        width: 160,
                        align: "center",
                        render: (h,context) => {
                            let riskToolType = context.row.riskToolType;
                            let riskToolTypeStr = riskToolType === 'scoreCard' ? '打分卡' : '预警规则';
                            return (
                                <span>{riskToolTypeStr}</span>
                            )
                        }
                    },
                    {
                        title: "描述",
                        key: "desc",
                        render: (h, context) => {
                            let vm = this;
                            let desc = context.row.riskTool.remark ? context.row.riskTool.remark : '';
                            return (
                                <span>{desc}</span>
                            )
                        }
                    },
                    {
                        title: "状态",
                        align: "center",
                        width: 100,
                        render: (h, context) => {
                            let vm = this;
                            let enable = context.row.isEnable;
                            let enableLabel = enable ? '启用' : '禁用';
                            let icon = h("Icon", {
                                props: {
                                    type: enable
                                        ? "checkmark-round"
                                        : "close-round",
                                },
                                class: enable
                                        ? "mr-2 text-success"
                                        : "mr-2 text-danger",
                            });

                            return h(
                                "div",
                                [icon, enableLabel]
                            );
                        }
                    },
                    {
                        title: "定时模式",
                        key: "jobCode",
                        width: 100
                    },
                    {
                        title: "操作",
                        width: 100,
                        align: 'center',
                        fixed: 'right',
                        render: (h, context) => {
                            let vm = this;
                            return (
                                <div>
                                    <i-button
                                        type="text"
                                        size="small"
                                        class="mr-1"
                                        title="编辑工具"
                                        icon="wrench"
                                        onClick={() => {
                                            let self = this,
                                                rowData = context.row,
                                                modal = self.RiskToolSlideModal;
                                            // 赋值
                                            modal.title = '编辑风控工具';
                                            modal.form.isAdd = false;
                                            modal.form.type = rowData.riskToolType;
                                            // 重新加载风控下拉框
                                            // self.switchRiskTool.call(self);
                                            modal.riskToolList = [];
                                            if(modal.form.type === 'scoreCard'){
                                                self.loadScoredList().then(function(resp){
                                                    modal.riskToolList = resp.data.data ? resp.data.data : [];
                                                    modal.form.riskTool = rowData.riskTool.oid;
                                                });
                                            }else{
                                                self.loadAlertRuleList().then(function(resp){
                                                    modal.riskToolList = resp.data.data ? resp.data.data : [];
                                                    modal.form.riskTool = rowData.riskTool.oid;
                                                });
                                            }

                                            modal.form.relationOid = rowData.oid;
                                            modal.form.switch = rowData.isEnable;
                                            modal.form.jobPattern = rowData.jobCode;
                                            modal.form.applyObject = 'customer';
                                            

                                            // 显示弹窗
                                            modal.show = true;
                                        }}
                                    ></i-button>
                                    <i-button
                                        type="text"
                                        size="small"
                                        class="mr-1"
                                        title="删除工具"
                                        icon="trash-b"
                                        onClick={() => {
                                             let self = this,
                                                rowData = context.row,
                                                tool = rowData.riskTool;

                                            self.$Modal.confirm({
                                                title: "系统提示",
                                                content: "<p>确认从当前节点移除“" + tool.name + "”工具？</p>",
                                                onOk: () => {


                                                    self.deleteRiskTool(rowData.oid).then(function(resp){
                                                        if(resp.code === 0){
                                                            // 删除成功提示
                                                            self.$Message.success("删除成功!");

                                                            // 刷新数据
                                                            self.table.data = [];
                                                            self.table.loading = true;
                                                            self.loadRiskTools().then((res) => {
                                                                self.table.loading = false;
                                                                self.table.data = res.data.data ? res.data.data : [];
                                                            });
                                                            
                                                        }else{
                                                            self.$Message.error("删除失败！");
                                                        }
                                                    });
                                                },
                                                onCancel: () => {
                                                    self.$Message.info("取消删除");
                                                }
                                            }); 
                                        }}
                                    ></i-button>
                                </div>
                            )
                        }
                    }
                ]
            },
        };
    },
    mounted() {
        let self = this;

        // 加载风控工具
        self.table.loading = true;
        self.loadRiskTools().then((res) => {
            self.table.loading = false;
            // console.log(res.data.data);
            self.table.data = res.data.data ? res.data.data : [];
        });

        // 加载风控工具调用模式
        self.loadJobPatternList();
        // 加载风控对象
        self.loadApplyObject();
        // 加载风控工具
        self.loadScoredList().then(function(resp){
            self.RiskToolSlideModal.riskToolList = resp.data.data ? resp.data.data : [];
        });
    },
    methods: {
        // 新增风控工具
        addRiskTool(){
            let self = this,
                riskNodeOid = self.row.oid,
                modalForm = self.RiskToolSlideModal.form,
                url = `/app/v1/riskNodes/${riskNodeOid}/riskTools`,
                param = {
                    'isEnable': modalForm.switch,
                    'jobCode': modalForm.jobPattern,
                    'riskObjectType': modalForm.applyObject,
                    'riskToolOid': modalForm.riskTool,
                    'riskToolType': modalForm.type
                };
            return new Promise(function(resolve,reject){
                self.$http.post(url,param).then(function(resp){
                    if(resp.code === 0){
                        resolve(resp);
                    }else{
                        reject(resp);
                    }
                });
            });
        },
        // 编辑风控工具
        editRiskTool(){
            let self = this,
                nodeOid = self.row.oid,
                modalForm = self.RiskToolSlideModal.form,
                relationOid = modalForm.relationOid,
                toolOid = modalForm.riskTool,
                url = `/app/v1/riskNodeTools/${relationOid}`,
                param = {
                    'isEnable': modalForm.switch,
                    'jobCode': modalForm.jobPattern,
                    'riskObjectType': modalForm.applyObject,
                    'riskToolOid': toolOid,
                    'riskToolType': modalForm.type
                };
                
            return new Promise(function(resolve,reject){
                self.$http.put(url,param).then(function(resp){
                    if(resp.code === 0){
                        resolve(resp);
                    }else{
                        reject(resp);
                    }
                });
            });
        },
        // 切换风控工具类型
        switchRiskTool(){
            let self = this,
                modal = self.RiskToolSlideModal,
                form = modal.form
            // 重新加载风控工具
            form.riskTool = null;
            modal.riskToolList = [];
            if(form.type === 'scoreCard'){
                self.loadScoredList().then(function(resp){
                    modal.riskToolList = resp.data.data ? resp.data.data : [];
                });
            }else{
                self.loadAlertRuleList().then(function(resp){
                    modal.riskToolList = resp.data.data ? resp.data.data : [];
                });
            }
        },
        // 加载预警规则
        loadAlertRuleList(){
            let self = this;
            return new Promise(function(resolve,reject){
                self.$http.get(`/app/v1/riskTool/alertRules?riskObjectType=customer`).then(function(resp){
                    if(resp.code === 0){
                        resolve(resp);
                    }else{
                        reject(resp);
                    }
                });
            });
        },
        // 加载风控工具
        loadScoredList(){
            let self = this;
            return new Promise(function(resolve,reject){
                self.$http.get(`/app/v1/riskTool/scoreCards?riskObjectType=customer`).then(function(resp){
                    if(resp.code === 0){
                        resolve(resp);
                    }else{
                        reject(resp);
                    }
                });
            });
        },
        // 加载风控对象
        loadApplyObject(){
            let self = this,
                url = `/app/v1/toolObjects`;

            self.RiskToolSlideModal.applyObjectList = [];
            self.$http.get(url).then(function(resp){
                if(resp.code === 0){
                    self.RiskToolSlideModal.applyObjectList = resp.data.data ? resp.data.data : [];
                }else{
                    self.$Message.error(resp.desc);   
                }
            }).catch(function(resp){
                self.$Message.error(resp.desc);
            });
        },
        // 加载风控工具的执行模式列表
        loadJobPatternList(){
            let self = this,
                url = `/app/v1/udc/langs/zh_CN/udcCodes/RISK_TOOL_JOB`;
            
            self.RiskToolSlideModal.jobPatternList = [];
            self.$http.get(url).then(function(resp){
                if(resp.code === 0){
                    self.RiskToolSlideModal.jobPatternList = resp.data ? resp.data : [];
                }else{
                    self.$Message.error(resp.desc);   
                }
            }).catch(function(resp){
                self.$Message.error(resp.desc); 
            });
        },
        deleteRiskTool(oid){
            let self = this;
            return new Promise(function(resolve,reject){
                self.$http.delete(`/app/v1/riskNodeTools/${oid}`).then(function(resp){
                    if(resp.code === 0){
                        resolve(resp);
                    }else{
                        reject(resp);
                    }
                });
            });
        },
        loadRiskTools(){
            let self = this;
            return new Promise(function(resolve,reject){
                self.$http.get('/app/v1/riskNodes/'+self.row.oid+'/riskTools').then(function(resp){
                // self.$http.get('/app/v1/riskNodes/1/riskTools').then(function(resp){
                    if(resp.code === 0){
                        resolve(resp);
                    }else{
                        reject(resp);
                    }
                });
            });
        }
    }
};
</script>
